package kburndown.client.app.chart
{
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import kburndown.client.service.data.DayResult;
	import kburndown.client.service.data.ProjectInfo;
	import kburndown.client.service.data.UnitInfo;
	import kburndown.client.app.utils.DateHelper;
	import kburndown.client.app.ViewState;
	
	import mx.charts.AxisRenderer;
	import mx.charts.CategoryAxis;
	import mx.charts.DateTimeAxis;
	import mx.charts.LineChart;
	import mx.charts.LinearAxis;
	import mx.charts.chartClasses.IAxis;
	import mx.charts.series.LineSeries;
	import mx.charts.series.items.LineSeriesItem;
	import mx.events.FlexEvent;

	[Event(name="seriesUpdateComplete", type="kburndown.client.app.chart.BurnDownChartEvent")]
	public class BurnDownChart extends LineChart
	{
		private var _viewState: String = ViewState.BurnDown;
		public function BurnDownChart()
		{
			super();
			focusEnabled = false;
		}
		
		public function get viewState(): String
		{
			return _viewState;
		}
		
		public function set viewState(value: String): void
		{
			_viewState = value;
			if(viewState == ViewState.BurnDown)
			{
				lineSeriesAll.visible = true;
				lineSeriesExpected.visible = true;
			}
			else
			{
				lineSeriesAll.visible = false;
				lineSeriesExpected.visible = false;
			}
		}
		
		public function updateHaxis(): void
		{
			var project: ProjectInfo = ProjectInfo.instance;
			xAxis = new DateTimeAxis();
			xAxis.interval = project.actualDuration;
			//延期的日子
			var worseDate: Date = ProjectInfo.instance.getWorseDay();
			if(worseDate.time > project.endDate.time)
			{
				xAxis.maximum = worseDate;	
			}
			this.horizontalAxis = xAxis;
			
			var yAxis: LinearAxis = this.verticalAxis as LinearAxis;
			if(viewState == ViewState.BurnDown)
			{
				yAxis.labelFunction = null;
			}	
			else
			{
				yAxis.labelFunction = _yAxisLabelFunction;
			}
			invalidateDisplayList();
		}
		
		private function _yAxisLabelFunction(labelValue:Object, previousValue:Object, axis:IAxis):String
		{
			return "     ";
		}
		
		public function get dataRectangle():Rectangle
		{
			var rect: Rectangle = dataRegion.clone();
			return rect;
		}
		
		public function get currentDateXSite(): Number
		{
			var today: Date = DateHelper.getToday();

			//实际上取的是明天的位置,所以dayCount要+1
			var dayCount: int = DateHelper.getDayCountBetween(ProjectInfo.instance.startDate, today) + 1;
			var todayXSite: Number = dataRegion.x + dayBlockWidth * dayCount;
			
			return todayXSite;
		}
		
		public function get currentDateRect(): Rectangle
		{
			var _dayBlockWidth: Number = dayBlockWidth;
			var xSite: Number = currentDateXSite - _dayBlockWidth;
			var ySite: Number = dataRegion.y + this.y;
			var _width: Number = _dayBlockWidth;
			var _heigth: Number = dataRegion.height;
			return new Rectangle(xSite, ySite, 
				_width, _heigth);
		}
		
		public function get dayBlockWidth(): Number
		{
			//整个项目所花时间
			return dataRectangle.width / ProjectInfo.instance.actualDuration;
		}
		
		
		private var lineSeriesAll: LineSeries;
		private var lineSeriesExpected: LineSeries;
		private var xAxis: DateTimeAxis ;
		
		
		public function setData(project: ProjectInfo): void
		{
			var dataProvider: Array = [];
			var duration: Number = project.duration;
			
			var dayResults: Vector.<DayResult> = project.dayResults;
			var dayResultsCount: int = dayResults.length;
			var allTime: Number = 0;
			for(var dayIndex: int = 0; dayIndex < dayResultsCount; dayIndex++)
			{
				var dayResult :Object = {};
				var date: Date = project.dayResults[dayIndex].date;
				dayResult.result = project.dayResults[dayIndex].value;
				dayResult.when = date;
				if(date.time == project.endDate.time)
					continue;
				if(dayIndex == 0)
				{
					allTime = project.dayResults[dayIndex].value;
				}	
				var dateCount: int = DateHelper.getDayCountBetween(date, project.endDate);
				dayResult["expected"] = allTime / duration * dateCount;
				dataProvider.push(dayResult);
			}
			var endResult: Object = {};
			endResult.when = project.endDate;
			endResult["expected"] = 0;
//			endResult["result"] = 0;
			dataProvider.push(endResult);
			
			var seriesArray: Array = [];
			lineSeriesExpected= new LineSeries();
			lineSeriesExpected.yField = "expected";
			lineSeriesExpected.xField = "when";
			seriesArray.push(lineSeriesExpected);
			lineSeriesAll = new LineSeries();
			lineSeriesAll.yField = "result";
			lineSeriesAll.xField = "when";
			lineSeriesAll.addEventListener(FlexEvent.UPDATE_COMPLETE, _seriesUpdateHandler);
			seriesArray.push(lineSeriesAll);
			
			updateHaxis();
			
			
			this.dataProvider = dataProvider;
			this.series = seriesArray;
			
		}
		
		private function _seriesUpdateHandler(event: Event): void
		{
			
		}
		
		//-------------------------------------------------------------------------
		//		
		//		提示线相关
		//
		//-------------------------------------------------------------------------
		private function updateLine(): void
		{
			graphics.clear();
			if(viewState != ViewState.BurnDown)
			{
				showTodayLine();
				var worseDay: Date = ProjectInfo.instance.getWorseDay();
				var endDay: Date = ProjectInfo.instance.endDate;
				if(worseDay.time >  endDay.time + DateHelper.OneDay)
					showEndDayLine();
			}
		}
		
		private function showTodayLine(): void
		{
			var today: Date = DateHelper.getToday();
			var dayCount: int = DateHelper.getDayCountBetween(ProjectInfo.instance.startDate, today);
			var todayXSite: Number = dataRegion.x + dayBlockWidth * dayCount;
			
			graphics.beginFill(0x003CFF, .6);
			graphics.drawRect(todayXSite, 12,  dayBlockWidth, dataRegion.height - 2);
			graphics.endFill();
		}
		
		private function showEndDayLine(): void
		{
			var endDayXSite: Number = dataRegion.x + dayBlockWidth * ProjectInfo.instance.duration;
			
			graphics.beginFill(0x991B1B, .7);
			graphics.drawRect(endDayXSite, 12,  dataRegion.width - dayBlockWidth * ProjectInfo.instance.duration, 
				dataRegion.height - 2);
			graphics.endFill();
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			updateLine();
		}
//		
	}
}